package com.xxzhy.myplay;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.xxzhy.myplay.util.RecorderVideoActivity;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String filePath = "";//文件名
    private final static int MYCODE = 1;
    private static final int VIDEO_CAPTURE = 0;
    private ImageView addVideo;
    private ImageView videoPlayIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btAddvideo = findViewById(R.id.bt_addvideo);
        addVideo = findViewById(R.id.add_video);
        videoPlayIv = findViewById(R.id.video_play_iv);
        btAddvideo.setOnClickListener(this);
        videoPlayIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_addvideo:

                if (Build.VERSION.SDK_INT >= 23) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                            || ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.RECORD_AUDIO}, MYCODE);
                    } else {
                        Intent intent2 = new Intent(MainActivity.this, RecorderVideoActivity.class);
                        startActivityForResult(intent2, VIDEO_CAPTURE);
                    }
                } else {
                    Intent intent2 = new Intent(MainActivity.this, RecorderVideoActivity.class);
                    startActivityForResult(intent2, VIDEO_CAPTURE);
                }

                break;
            case R.id.video_play_iv:
                if (!"".equals(filePath)) {
                    Intent intents = new Intent(Intent.ACTION_VIEW);
                    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intents.setDataAndType(Uri.fromFile(new File(filePath)), "video/*");
                    startActivity(intents);
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 1 && requestCode == VIDEO_CAPTURE) {
            String str = data.getStringExtra("file");
            filePath = str;
            addVideo.setImageBitmap(getBitmap(filePath));
            videoPlayIv.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 获得视频的缩略图
     */
    public Bitmap getBitmap(String imgPath) {
        if (getAndroidSDKVersion() >= 8) {
            Bitmap bp = ThumbnailUtils.createVideoThumbnail(imgPath, MediaStore.Video.Thumbnails.MINI_KIND);
            return bp;
        } else {
            return null;
        }
    }

    /**
     * 当前系统的版本号
     * @author wyf
     * @create 2016-10-12
     */
    public int getAndroidSDKVersion() {
        int version;
        try {
            version = Integer.valueOf(Build.VERSION.SDK);
        } catch (NumberFormatException e) {
            return -1;
        }
        return version;
    }
}
